package com.winit.schedule.job.tracker.repository;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.winit.schedule.core.model.JobID;
import com.winit.schedule.core.repository.constants.TaskStatus;
import com.winit.schedule.core.repository.entity.*;
import com.winit.schedule.core.repository.query.*;

/**
 * 控制台数据管理
 * 
 * @author jianke.zhang 2015-4-16 下午6:26:30
 * @since 1.0
 */
public interface ConsoleDataManager extends LogDataManager {

    List<TaskEntity> findTaskEntity(TaskQuery query);

    Map<String, Object> queryTaskRunDetails(String taskId);

    List<TaskTrackerEntity> queryTaskTrackers(TaskTrackerQuery query);

    TaskEntity getTaskById(String taskId);

    String saveTask(TaskEntity task);

    void updateTaskStatus(String taskId, TaskStatus status);

    long findRunningTaskItemNumByTaskId(String taskId);

    List<JobEntity> queryAllJob(JobQuery job);

    void saveJob(JobEntity job, List<String> taskIds) throws ParseException;

    void startJob(JobID jobId);

    void stopJob(JobID jobId);

    void delJob(JobID jobId);

    JobEntity queryJobById(String jobId);

    List<TaskEntity> queryTasksNotRelatedJob();

    List<TaskEntity> queryTasksRelatedJob(JobID jobId);

    long findRunningTaskItemNumByJobId(JobID jobId);

    Page queryTaskTrackerLog(String taskTrackerUuid, long startDate, long endDate, int page, int rows);

    Page queryJobLog(UserLogQuery userLogQuery, int page, int rows);

    Page queryTaskTrackerDisconnectLog(String ip, long startDate, long endDate, int page, int rows);

    Page queryTaskExeFailureLog(String jobId, String taskId, long startDate, long endDate, int page, int rows);

    Page queryTaskLog(LogQuery query);

    Page queryTaskRunRecord(String jobId, String taskId, long startDate, long endDate, int page, int rows);

    /**
     * 查询状态异常的taskItems
     * 
     * @param query
     * @return
     */
    List<TaskItemEntity> queryTaskItemsByTimeout(TaskItemQuery query);

    /**
     * 根据超时时间和id恢复状态异常的任务项
     * 
     * @param timeout 超时时间
     * @param ids taskItemId
     */
    void recoveryTaskItemByIds(long timeout, String[] ids);
    
    /**
     * 查询定时任务运行时间超过一定时长的任务
     * @param query
     * @return
     */
    List<TaskItemEntity> queryTaskItemsForLongtime(TaskItemQuery query);
    
    void removeTaskTrackerAndTaskDefines(String taskTrackerUuid);

    boolean hasTaskItem(String taskId);

    /********************************* user ********************************* */
    /**
     * 分页查询用户信息
     * @param query
     * @return
     */
    Page queryUserPage(UserQuery query, int page, int rows);

    /**
     * 根据用户名查询用户信息
     * @param username
     * @return
     */
    UserEntity queryUserByUsername(String username);

    /**
     * 修改用户密码
     * @param user
     */
    void updateUserPws(String username, String password);

    /**
     * 根据用户名和密码查询用户信息
     */
    UserEntity findByUsernameAndPassword(String username, String password);

    /**
     * 更新用户信息
     * @param user
     */
    void updateUser(UserEntity user);

    /**
     * 保存用户
     * @param user
     */
    void saveUser(UserEntity user);
}
